package oop.ex3.messages;

import java.io.IOException;

import oop.ex3.messages.exception.UnexpectedMessageEndException;
import oop.ex3.sockets.ClientSocket;

public class SimpleMessage implements MessageInterface {
	
	protected static final String END = "END";
	public static String[] _headers = {ValidHeaders.GOODBYE, 
		ValidHeaders.GOAWAY, ValidHeaders.WANTALLFILES, 
		ValidHeaders.WANTSERVERS, ValidHeaders.WELCOME,
		ValidHeaders.DONE, ValidHeaders.ENDLIST, ValidHeaders.ENDSESSION};
	
	protected String _header;
	
	public SimpleMessage(String msgName){
		_header = msgName;
	}

	public String getHeader(){
		return _header;
	}
	
	public SimpleMessage readMessage(ClientSocket socket) 
	throws UnexpectedMessageEndException, IOException{		
		System.out.println("IN <from: "+socket.getPort()+"> "+_header);
		if (!socket.readUTF().equals(END)){
			throw new UnexpectedMessageEndException();
		}
		return this;
	}

	public void sendMessage(ClientSocket socket) throws IOException {
		System.out.println("OUT <to: "+socket.getPort()+"> "+_header);
		socket.writeToSocket(_header);
	}
}
